Excel BI - Excel Challenge 797

excel-challenges
excel-formulas
🔰 Answer Expected Cage Animals A, 2 Tiger A B, 5 Lion C, 1 Elephant
Published

March 24, 2026

Illustration for Excel BI - Excel Challenge 797

Challenge Description

🔰 Answer Expected Cage Animals A, 2 Tiger A B, 5 Lion C, 1 Elephant

Solutions

library(tidyverse)
library(readxl)

path = "Excel/700-799/797/797 Cage Alignment.xlsx"
input1 = read_excel(path, range = "A2:A6")
input2 = read_excel(path, range = "B2:B32")
test  = read_excel(path, range = "D2:E14")

i1 = input1 %>%
  separate_wider_delim(cols = 1, delim = ", ", names = c("Cage", "volume")) %>%
  uncount(as.numeric(volume)) %>%
  mutate(rn = row_number())

i2 = input2 %>%
  mutate(rn = row_number())

result = left_join(i1, i2, by = "rn") %>%
  select(-c(rn, volume)) %>%
  mutate(Cage = ifelse(row_number() == 1, Cage, NA_character_), .by = Cage)

all.equal(result, test)
# > [1] TRUE
  • Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Parse the packed text or string structure; Aggregate or rank the data at the required grouping level.
  • Strengths: The code maps the workbook rule into a compact, reproducible pipeline.
  • Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
  • Gem: The elegant part is how little code is needed once the correct intermediate representation is chosen.
import pandas as pd

path = "700-799/797/797 Cage Alignment.xlsx"
input1 = pd.read_excel(path, usecols="A", skiprows=1, nrows=4)
input2 = pd.read_excel(path, usecols="B", skiprows=1, nrows=31)
test = pd.read_excel(path, usecols="D:E", skiprows=1, nrows=12).rename(columns=lambda c: c.replace('.1', ''))

i1 = input1['Cage'].str.split(", ", expand=True)
i1 = i1.loc[i1.index.repeat(i1[1].astype(int))].reset_index(drop=True)
i1["rn"] = i1.index + 1
i1.columns = ["Cage", "volume", "rn"]

i2 = input2.copy()
i2["rn"] = i2.index + 1

result = pd.merge(i1, i2, on="rn", how="left").drop(columns=["rn", "volume"])
result["Cage"] = result.groupby("Cage")["Cage"].transform(lambda x: x.where(x.index == x.index.min(), None))

print(result.equals(test)) # True

The Python version follows the same grouped logic and keeps the transformation explicit in a dataframe pipeline.

Difficulty Level

Easy / Medium

The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.